package com.hyjx.framework.common.util;

import com.hyjx.framework.service.CacheService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PinyinUtil {
	static Logger log = LoggerFactory.getLogger(PinyinUtil.class);
	private static CacheService cl = new CacheService();
	public static String getFirstChars(String chinese) {
		String ret = "";
		if (chinese == null)
			return ret;
		String key = "pinyin_" + chinese;
		if (cl.get(key) != null)
			return (String) cl.get(key);
		else {

			for (char c : chinese.toCharArray()) {
				if (isChinesse(c)) {
					try {
						ret += PinyinHelper.toHanyuPinyinStringArray(c)[0].charAt(0);
					} catch (Exception ex) {
						log.warn("字符" + c + "不是汉字。");
					}
				} else
					ret += c;
			}
			cl.set(key, ret.toUpperCase());
			return ret.toUpperCase();
		}
	}


	public static boolean isChinesse(char c) {
		byte[] bytes = String.valueOf(c).getBytes();
		if (bytes.length > 1) {
			int[] ints = new int[2];
			ints[0] = bytes[0] & 0xff;
			ints[1] = bytes[1] & 0xff;
			if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {
				return true;
			}
		}
		return false;
	}
	public static void main(String args[])
	{

	}
}
